from __future__ import absolute_import, division

from .base import *


class ResourceLoader(Object):
    path   = Attribute()
    loader = Attribute()
    
    scenario_gui = Attribute()
    cache        = DictAttribute()
    
    def font_path(self, name):
        return os.path.join(self.path, 'Fonts', '%s.ttf' % name)
    
    def icon_path(self, name):
        return os.path.join(self.path, 'Icons', '%s.png' % name)
    
    def texture_path(self, name):
        return os.path.join(self.path, 'Textures', '%s.png' % name)
    
    def model_path(self, name):
        return os.path.join(self.path, 'Models', '%s.egg' % name)
    
    def gameset_icon_path(self, name):
        return self.scenario_gui.icon_path(name)
    
    def gameset_chrome_path(self, name):
        return self.texture_path(name)   # FIXME: self.scenario_gui.chrome_path(name)
    
    def load_data(self, path, loader):
        if path in self._cache:
            return self._cache[path]
        
        self._cache[path] = loader(os.path.join(self.path, path))
        
        return self._cache[path]
    
    def load_shader(self, name):
        return self.load_data(os.path.join('Shaders', '%s.sha' % name), self.loader.loadShader)
    
    def load_texture(self, name):
        return self.load_data(self.texture_path(name), self.loader.loadTexture)
    
    @memoized
    def load_font(self, name, line_height = 1.1):
        FontPool.releaseAllFonts()
        
        return self.loader.loadFont(self.font_path(name), lineHeight = line_height)
    
    @memoized
    def load_gui_texture(self, name):
        image   = PNMImage(self.gameset_chrome_path(name))
        texture = Texture()
        
        if not image.isValid():
            image = PNMImage(16, 16)
            image.fill(0)
            image.alphaFill(0)
        
        texture.load(image)
        
        return texture
    
    @memoized
    def load_button_texture(self, name, state, image):
        if state.startswith('Clicked'):
            delta = 7
        else:
            delta = 6
        
        button = PNMImage(self.gameset_chrome_path(name + ' ' + state))
        
        if not os.path.isfile(self.gameset_icon_path(image)):
            delta += 6
            added  = PNMImage(self.icon_path('Unknown'))
        else:
            added  = PNMImage(self.gameset_icon_path(image))
        
        button.blendSubImage(added, delta, delta)
        
        texture = Texture()
        texture.load(button)
        
        return texture
    
    @memoized
    def load_float_texture(self, name, image):
        delta = 7
        float = PNMImage(self.gameset_chrome_path(name))
        
        if not os.path.isfile(self.gameset_icon_path(image)):
            delta += 6
            added  = PNMImage(self.icon_path('Unknown'))
        else:
            added  = PNMImage(self.gameset_icon_path(image))
        
        float.blendSubImage(added, delta, delta)
        float.blendSubImage(added, float.getXSize() - added.getXSize() - delta, delta)
        
        texture = Texture()
        texture.load(float)
        
        return texture
    
    def load_terrain(self, name):
        terrain = self.load_texture(name)
        terrain.setMinfilter(Texture.FTLinearMipmapLinear)
        terrain.setMagfilter(Texture.FTLinearMipmapLinear)
        
        return terrain
    
    def load_model(self, name):
        return self.loader.loadModel(self.model_path(name))
